The Touch Controller (attached to the Gameboard prefab game object) exposes all the touch features available to creators. This allows creators to get information on the objects touching the board.
The Gameboard prefab is always available in your game as long as the SDK has been integrated. In order to access it from our custom Mono Behavior we just need to find it by its tag.
GameObject gameboardObject = GameObject.FindWithTag("Gameboard");
With access to our Gameboard object we can then move on to obtain a reference to the Touch Controller. This is the controller that handles all the functions related to touch.
We will first need to define a reference to the controller to make it available throughout our script:
TouchController touchController;
With our listener defined, we can now proceed to listen for touch events, captured as shapes. In our case we'll have functions called OnShapeFound, OnShapeUpdated, OnShapeLost, and AllShapesLost to handle these events.
touchController = gameboardObject.GetComponent<TouchController>();
touchController.OnShapeFound += OnShapeFound;
touchController.OnShapeUpdated += OnShapeUpdated;
touchController.OnShapeLost += OnShapeLost;
touchController.AllShapesLost += AllShapesLost;
Now that we are able to receive the touch events, all that remains is to handle them in the game.
void OnShapeFound(GameboardShape shape)
{
//Handle shape found
}
void OnShapeUpdated(GameboardShape shape)
{
//Handle shape updated
}
void OnShapeLost(GameboardShape shape)
{
//Handle shape lost
}
void AllShapesLost()
{
//Handle all shapes lost (no touch on the board)
}
GameboardShape
PropertiesDataTypes.ShapeType
indicating what the shape is (see below)Vector2
indicating the screen position of the objectVector3
indicating the world position of the object based on either the camera specified on the touch controller, the game camera specified on the Gameboard prefab, or the main camera if neither of those are supplied. If unable to find a camera, this will be null.Vector2[]
with positions of the contour for this board object as represented in 2D, in World Space.Vector3[]
with positions of the contour for this board object in World Space.Vector3
indicating the world positions of the contour based on either the camera passed or the main camera. If unable to find a camera, this will return a default vector (0,0,0).Vector3[]
with positions of the countour for this board object in Local Space in relation to the sceneWorldPosition of this shape.Vector3
indicating the positions of the contour for this object in relation to the sceneWorldPosition of this shape based on either the camera passed or the main camera. If unable to find a camera, this will return a default vector (0,0,0).You can get specific shapes based on their id, or get all of the currently active shapes on the board.
In this example, imagine 123
is the current sessionId for a specific shape.
GameboardShape shape = touchController.GetShape(123);
You can also get all of the current shapes touching the board:
List<GameboardShape> shapes = touchController.GetShapes();
Based on v3.0.0+
You can also get the cotour information for the shapes that are on the board. This can be used to draw an outline around the shapes of the board, or to potentially create detailed collision meshes.
NOTE: In v3.0.0, the skipContourLogic
flag is true by default on the Gameboard prefab object. That means the extra logic to process the contours will not be done. If you want to use contours, make sure that flag is set to false.
You can use the GameboardCreatorAssist to access the static method BuildMeshFromTrackedObject
, this takes a GameboardShape
and the location the shape in in to return a mesh object built with the contours. You can also provide the camera and distance from camera so the mesh is in the correct location based on your camera.
Mesh mesh = GameboardCreatorAssist.BuildMeshFromTrackedObject(shape, content.transform, new Mesh(), distanceFromCamera, gameCamera);
Or, you can access the contour values directly on the GameboardShape object to use as you would like. In this example, we are adding lines showing where the contours are.
The UpdateContour
method would be called in the OnShapeUpdated listener method.
private void UpdateContour(GameboardShape shape)
{
GameObject lineRendererObject = new GameObject("DrawContourLine", typeof(LineRenderer));
var lineRenderer = lineRendererObject.GetComponent<LineRenderer>();
lineRenderer.startWidth = 0.1f;
lineRenderer.endWidth = 0.1f;
lineRenderer.loop = true;
var contourWorldVectors3D = shape.GetContourWorldVectors(Camera.main.transform.position.y);
lineRenderer.positionCount = contourWorldVectors3D.Length;
for (int i = 0; i < contourWorldVectors3D.Length; i++)
{
lineRenderer.SetPosition(i, contourWorldVectors3D[i]);
}
}
Although we are working in a managed environment it is always a good idea to clean the listeners when no longer needed.
void OnDestroy()
{
touchController.OnShapeFound -= OnShapeFound;
touchController.OnShapeUpdated -= OnShapeUpdated;
touchController.OnShapeLost -= OnShapeLost;
touchController.AllShapesLost -= AllShapesLost;
}
This section include the entire code in one single, easy to copy section.
TouchController touchController;
// Start is called before the first frame update
void Start()
{
GameObject gameboardObject = GameObject.FindWithTag("Gameboard");
touchController = gameboardObject.GetComponent<TouchController>();
touchController.OnShapeFound += OnShapeFound;
touchController.OnShapeUpdated += OnShapeUpdated;
touchController.OnShapeLost += OnShapeLost;
touchController.AllShapesLost += AllShapesLost;
}
void OnShapeFound(GameboardShape shape)
{
//Handle shape found
}
void OnShapeUpdated(GameboardShape shape)
{
//Handle shape updated
}
void OnShapeLost(GameboardShape shape)
{
//Handle shape lost
}
void AllShapesLost()
{
//Handle all shapes lost (no touch on the board)
}
void OnDestroy()
{
touchController.OnShapeFound -= OnShapeFound;
touchController.OnShapeUpdated -= OnShapeUpdated;
touchController.OnShapeLost -= OnShapeLost;
touchController.AllShapesLost -= AllShapesLost;
}